#define BLINKER_WIFI
#define BLINKER_MIOT_OUTLET

#include <Blinker.h>
#include "wificonfig.h"


char auth[] = "3c239619fe98";
char ssid[] = "WHY";
char pswd[] = "WHYwhy12345";

// 电脑电脑电源按键引脚定义
#define PWDPin 4    //  电脑电源按键引脚D1 高电平导通
// 电脑电脑重启按键引脚定义
#define RSTPin 5    //  电脑重启按键引脚D2 高电平导通
// 电脑开机状态检测引脚定义
#define POWERSTATUS_Pin 13 //  电脑开机状态检测引脚D7

// WIFI网络配置引脚定义
#define WIFIPin 12 //  WIFI网络配置引脚D6

bool oState = false;
bool LedFlashFlag = false;

// 配网时led闪烁
#define TimerCycle  500             //软件定时器周期500ms
uint32_t TimerTick = 0;
// 按键长按配网时间
uint32_t KeyPressTime = 0;          //按键重新配网计时器
#define KeylongPressCycle 10       //长按配网时间


BlinkerButton PWDBtn("btn_pwd");   //电脑电源按键组件
BlinkerButton RSTBtn("btn_rst");   //电脑重启按键组件

// 电脑开机
void ComputerPower_ON(void)
{
    digitalWrite(PWDPin, HIGH);
    delay(500);
    digitalWrite(PWDPin, LOW);

}
void ComputerPower_OFF(void)
{
    digitalWrite(PWDPin, HIGH);
    delay(1000);
    digitalWrite(PWDPin, LOW);
}
void ComputerReset(void)
{
    digitalWrite(RSTPin, HIGH);
    delay(500);
    digitalWrite(RSTPin, LOW);

}

// 按下电脑电源按键即会执行该函数
void PWDBtn_callback(const String &state)
{
    BLINKER_LOG("get pwd button state: ", state);
    Serial.printf("get pwd button state %s oState:%d\n",state,oState);
     if (state == BLINKER_CMD_ON) {

        if (oState != true)
        {
            digitalWrite(LED_BUILTIN, LOW);
            BlinkerMIOT.powerState("on");
            BlinkerMIOT.print();

            
            PWDBtn.color("Green");
            PWDBtn.print("on");
            ComputerPower_ON();
        }
        oState = true;
    }
    else if (state == BLINKER_CMD_OFF) {

        // BlinkerMIOT.powerState("off");
        // BlinkerMIOT.print();
        if (oState != false)
        {
            digitalWrite(LED_BUILTIN, HIGH);
            PWDBtn.color("#D2B48C");
            PWDBtn.print("off");
            PWDBtn.text("电脑关机中...");
            ComputerPower_OFF();
        }
        
        // oState = false;
    }
    
}
// 按下电脑重启按键即会执行该函数
void RSTBtn_callback(const String &state)
{
    BLINKER_LOG("get rst button state: ", state);
    Serial.printf("get pwd button state %s\n",state);

    ComputerReset();
}

// 发送按键状态
void send_status(bool state)
{
    if (state == true)
    {
        PWDBtn.color("Green");
        PWDBtn.print("on");
        PWDBtn.text("电脑已开机");
        BlinkerMIOT.powerState("on");
    }
    else if(state == false)
    {
        PWDBtn.color("Red");
        PWDBtn.print("off");
        PWDBtn.text("电脑已关机");
        BlinkerMIOT.powerState("off");
    }
}

// 心跳包
void heartbeat()
{
    if (oState == true)
    {
        PWDBtn.color("Green");
        PWDBtn.print("on");
        PWDBtn.text("电脑已开机");
        BlinkerMIOT.powerState("on");
    }
    else if(oState == false)
    {
        PWDBtn.color("Red");
        PWDBtn.print("off");
        PWDBtn.text("电脑已关机");
        BlinkerMIOT.powerState("off");
    }
    
}


void miotPowerState(const String & state)
{
    BLINKER_LOG("need set power state: ", state);

    if (state == BLINKER_CMD_ON) {
        digitalWrite(LED_BUILTIN, LOW);

        BlinkerMIOT.powerState("on");
        BlinkerMIOT.print();
        
        PWDBtn.color("Green");
        PWDBtn.print("on");
        if (oState != true)
        {
            ComputerPower_ON();
        }
        
        oState = true;
    }
    else if (state == BLINKER_CMD_OFF) {
        digitalWrite(LED_BUILTIN, HIGH);

        BlinkerMIOT.powerState("off");
        BlinkerMIOT.print();

        PWDBtn.color("Red");
        PWDBtn.print("off");
        if (oState != false)
        {
            ComputerPower_OFF();
        }

        oState = false;
    }
}

void miotQuery(int32_t queryCode)
{
    BLINKER_LOG("MIOT Query codes: ", queryCode);

    switch (queryCode)
    {
        case BLINKER_CMD_QUERY_ALL_NUMBER :
            BLINKER_LOG("MIOT Query All");
            BlinkerMIOT.powerState(oState ? "on" : "off");
            BlinkerMIOT.print();
            break;
        case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
            BLINKER_LOG("MIOT Query Power State");
            BlinkerMIOT.powerState(oState ? "on" : "off");
            BlinkerMIOT.print();
            break;
        default :
            BlinkerMIOT.powerState(oState ? "on" : "off");
            BlinkerMIOT.print();
            break;
    }
}

void dataRead(const String & data)
{
    BLINKER_LOG("Blinker readString: ", data);

    Blinker.vibrate();
    
    uint32_t BlinkerTime = millis();
    
    Blinker.print("millis", BlinkerTime);
}

void setup()
{
    Serial.begin(115200);
    BLINKER_DEBUG.stream(Serial);

    // 初始化电机IO
    pinMode(PWDPin, OUTPUT);
    digitalWrite(PWDPin, LOW);

    pinMode(RSTPin, OUTPUT);
    digitalWrite(RSTPin, LOW);

    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, LOW);

    // Blinker.begin(auth, ssid, pswd);

    // 连接wifi
    loadConfig();
    WiFi.hostname("电脑开机卡");
    connectWiFi();

    if (RunMode == Normal)
    {
        Blinker.attachData(dataRead);

        // 注册心跳包
        Blinker.attachHeartbeat(heartbeat);

        // 注册按键
        PWDBtn.attach(PWDBtn_callback);
        RSTBtn.attach(RSTBtn_callback);
        
        BlinkerMIOT.attachPowerState(miotPowerState);
        BlinkerMIOT.attachQuery(miotQuery);
        Serial.println("Wifi connect success");
        digitalWrite(LED_BUILTIN, HIGH);

    }
    else if(RunMode == ReConnectWifi)
    {
        Serial.println("Wifi connect error");
    }
    

}

void loop()
{
    server.handleClient();
    dnsServer.processNextRequest();

    if (RunMode == Normal)
    {
        Blinker.run();
    }
    
    if (TimerTick == 0 || (millis() - TimerTick) >= TimerCycle)
    {
        TimerTick = millis();
        if (digitalRead(POWERSTATUS_Pin))
        {
            KeyPressTime++;
            if (KeyPressTime >= 10) //5s
            {
                KeyPressTime = 0;
                oState = true;
                send_status(oState);
                Serial.printf("computer is power on oState:%d\n",oState);
            }
        }
        else if (!digitalRead(POWERSTATUS_Pin))
        {
            KeyPressTime++;
            if (KeyPressTime >= 10) //5s
            {
                KeyPressTime = 0;
                oState = false;
                send_status(oState);
                Serial.printf("computer is power off oState:%d\n",oState);
            }
        }
        
        if (!digitalRead(WIFIPin) && RunMode == Normal)
        {
            KeyPressTime++;
            Serial.printf("KeyPressTime:%d\n",KeyPressTime);

            if (KeyPressTime >= KeylongPressCycle)
            {
                KeyPressTime = 0;
                // 进入配网模式
                RunMode = ReConnectWifi;
                Resetwifiinfo();
            }
            
        }
        // wifi配网过程中间隔500ms闪烁
        else if(RunMode == ReConnectWifi)
        {
            Serial.printf("LedFlashFlag:%d\n",LedFlashFlag);
            
            if (LedFlashFlag == true)
            {
                LedFlashFlag = false;
                digitalWrite(LED_BUILTIN, HIGH);
            }
            else
            {
                LedFlashFlag = true;
                digitalWrite(LED_BUILTIN, LOW);
            }
        }
    }
    
}
